﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entitty;

public partial class Admin_ucControl_ucContact : System.Web.UI.UserControl
{
    TblContactDAO contactDAO = new TblContactDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadContact();
            string id = Request.QueryString["id"];
            if (id == "" || id == null)
            {
                panel1.Visible = false;
            }
            else
            {
                panel1.Visible = true;
                LoadDetailContact();
            }
        }
    }

    private void LoadContact()
    {
        grdLienHe.DataSource = contactDAO.GetAll();
        grdLienHe.DataBind();
    }
    private void LoadDetailContact()
    {
        string id = Request.QueryString["id"];
        TblContact contact = contactDAO.GetContactById(Global.ConvertToInt(id));
        txtEmail.Text = contact.Email;
        txtPhone.Text = contact.Phone;
        txtYahoo.Text = contact.Yahoo;
    }

    protected void save_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        TblContact contact = new TblContact();
        contact.Email = txtEmail.Text.Trim();
        contact.Phone = txtPhone.Text.Trim();
        contact.Yahoo = txtYahoo.Text.Trim();
        if (id == "" || id == null)
        {
            contactDAO.Insert(contact, 1);
        }
        else
        {
            contact.Id = Global.ConvertToInt(id);
            contactDAO.Insert(contact, 2);
        }
    }
}
